home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 62 / Quick PC 62.iso / I386 / IIS5_01.CAB / IIS_date.inc < prev    next >
Encoding:
Text File  |  1998-09-16  |  7.9 KB  |  341 lines

  1. <%
  2.  
  3. ' Use these constants as parameters to the UIDateFormat constructor to
  4. ' determine the format used in getDate()
  5. Const DATEFORMAT_LONG = 0
  6. Const DATEFORMAT_SHORT = 1
  7.  
  8. %>
  9.  
  10. <SCRIPT LANGUAGE="JavaScript">
  11.  
  12. MONTHS = new Array()
  13. MONTHS[0] = "<%= L_JAN_TEXT %>";
  14. MONTHS[1] = "<%= L_FEB_TEXT %>";
  15. MONTHS[2] = "<%= L_MAR_TEXT %>";
  16. MONTHS[3] = "<%= L_APR_TEXT %>";
  17. MONTHS[4] = "<%= L_MAY_TEXT %>";
  18. MONTHS[5] = "<%= L_JUN_TEXT %>";
  19. MONTHS[6] = "<%= L_JUL_TEXT %>";
  20. MONTHS[7] = "<%= L_AUG_TEXT %>";
  21. MONTHS[8] = "<%= L_SEP_TEXT %>";
  22. MONTHS[9] = "<%= L_OCT_TEXT %>";
  23. MONTHS[10] = "<%= L_NOV_TEXT %>";
  24. MONTHS[11] = "<%= L_DEC_TEXT %>";
  25.  
  26. DAYS = new Array()
  27. DAYS[0] = "<%= L_SUN_TEXT %>";
  28. DAYS[1] = "<%= L_MON_TEXT %>";
  29. DAYS[2] = "<%= L_TUE_TEXT %>";
  30. DAYS[3] = "<%= L_WED_TEXT %>";
  31. DAYS[4] = "<%= L_THU_TEXT %>";
  32. DAYS[5] = "<%= L_FRI_TEXT %>";
  33. DAYS[6] = "<%= L_SAT_TEXT %>";
  34.  
  35. DAYTIME = new Array()
  36. DAYTIME[0] = "<%= L_MORNING_TEXT %>"
  37. DAYTIME[1] = "<%= L_AFTERNOON_TEXT %>"
  38.  
  39. // UIDateFormat
  40. //
  41. // Date conversion class. To use: create a UIDateFormat object
  42. // with a flag indicating the type of date formatting desired
  43. // (DATEFORMAT_LONG or DATEFORMAT_SHORT) and use the getDate()
  44. // and getTime() methods to return a locale formatted date.
  45. function UIDateFormat( fDateFormat )
  46. {
  47.     // Public interface - use these methods to display localized dates.
  48.     // Both methods take a single JavaScript Date object as a parameter.
  49.     this.getDate = uidGetDate;
  50.     this.getTime = uidGetTime;
  51.  
  52.     // Implementation
  53.     this.iDaytimeIndex = 0;
  54.     this.strTimeFormat = "<%= L_TIME_FORMAT_TEXT %>";
  55.  
  56.     // strDate format can be changed to point to the long or short
  57.     // date format as needed by the particular page.
  58.     if( fDateFormat == <%= DATEFORMAT_LONG %> )
  59.     {
  60.         this.strDateFormat = "<%= L_DATE_LONGFORMAT_TEXT %>";
  61.     }
  62.     else
  63.     {
  64.         this.strDateFormat = "<%= L_DATE_SHORTFORMAT_TEXT %>";
  65.     }
  66.     this.strTimeTokens = "hHms";
  67.     this.strDateTokens = "dMy";
  68.     
  69.     // helper methods
  70.     this.uidTimeStringPartFromToken = uidTimeStringPartFromToken;
  71.     this.uidDateStringPartFromToken = uidDateStringPartFromToken;
  72. }
  73.  
  74. // getNeutralDateString
  75. //
  76. // Global utility function. Formats a date into a neutral format m/d/yyyy.
  77. // This shouldn't be displayed, but rather used to save the date
  78. // part in a format that doesn't lose any information.
  79. function getNeutralDateString( date )
  80. {
  81.     var theYear = date.getYear();
  82.     if( theYear < 1000 )
  83.     {
  84.         theYear += 1900;
  85.     }
  86.     var theMonth = date.getMonth() + 1;
  87.  
  88.     return "" + theMonth + "/" + date.getDate() + "/" + theYear;
  89. }
  90.  
  91. // uidGetDate
  92. //
  93. // Returns the date part of the string. Called through UIDate.getDate().
  94. // Make a single pass through the date format string, extracting format
  95. // tokens. Uses uidDateStringPartFromToken() to interpret found tokens.
  96. //
  97. // date - the date value being translated
  98. function uidGetDate( date )
  99. {
  100.     var strOut = "";
  101.     var i = 0, j = 0;
  102.     var nFormatStrLen = this.strDateFormat.length;
  103.     var c;
  104.     var nTokenType = -1;
  105.  
  106.     for( i = 0; i < nFormatStrLen; i++ )
  107.     {
  108.         c = this.strDateFormat.charAt(i);
  109.         nTokenType = this.strDateTokens.indexOf( c );
  110.         if(  nTokenType != -1 )
  111.         {
  112.             // We found a token
  113.             
  114.             // Move to the next position after the current token.
  115.             for( j = i + 1; j < nFormatStrLen; j++ )
  116.             {
  117.                 if( c != this.strDateFormat.charAt(j) )
  118.                 {
  119.                     break;
  120.                 }
  121.             }
  122.             strOut += this.uidDateStringPartFromToken( date, nTokenType, j - i );
  123.             i = j - 1;
  124.         }
  125.         else if( c == "'" )
  126.         {
  127.             // Interpret characters between single quotes as multichar literals
  128.  
  129.             // Skip over characters between the single quotes
  130.             for( j = i + 1; j < nFormatStrLen; j++ )
  131.             {
  132.                 if( c == this.strDateFormat.charAt(j) )
  133.                 {
  134.                     break;
  135.                 }
  136.             }
  137.             strOut += this.strDateFormat.substring( i + 1, j );
  138.             i = j;
  139.         }
  140.         else
  141.         {
  142.             // literal character not quote delimited.
  143.             strOut += c;
  144.         }
  145.     }
  146.     return strOut;
  147. }
  148.  
  149. // uidDateStringPartFromToken
  150. //
  151. // Returns a string representing the localized value of the date part
  152. // indicated by nTokenType.
  153. //
  154. // date - the javascript date value being translated
  155. // nTokenType - position in token string - "dMy"
  156. // nTokenChars - number of characters comprising the token
  157. function uidDateStringPartFromToken( date, nTokenType, nTokenChars )
  158. {
  159.     var strOut = "";
  160.     var datePart;
  161.  
  162.     if( nTokenType == 0 )
  163.     {
  164.         // Days
  165.         if( nTokenChars > 2 )
  166.         {
  167.             // The day of the week
  168.             strOut += DAYS[date.getDay()];
  169.         }
  170.         else
  171.         {
  172.             // The day of the month
  173.             datePart = date.getDate();
  174.             if( datePart < 10 && nTokenChars == 2 )
  175.             {
  176.                 strOut += "0";
  177.             }
  178.             strOut += datePart;
  179.         }
  180.     }
  181.     else if( nTokenType == 1 )
  182.     {
  183.         // Months
  184.         datePart = date.getMonth();
  185.         if( nTokenChars > 2 )
  186.         {
  187.             // Month as string
  188.             strOut += MONTHS[datePart];
  189.         }
  190.         else
  191.         {
  192.             // Month as integer
  193.             datePart++;
  194.             if( datePart < 10 && nTokenChars == 2 )
  195.             {
  196.                 strOut += "0";
  197.             }
  198.             strOut += datePart;
  199.         }
  200.     }
  201.     else if( nTokenType == 2 )
  202.     {
  203.         // Year
  204.         
  205.         // Get the full year as a workaround for JavaScript bogusness.
  206.         datePart = date.getYear();
  207.         if( datePart < 1000 )
  208.         {
  209.             datePart += 1900;
  210.         }
  211.         strOut += datePart;
  212.         if( nTokenChars < 4 )
  213.         {
  214.             // two digit year
  215.             strOut = strOut.substring( 2, 4 );
  216.         }
  217.     }
  218.     return strOut;
  219. }
  220.  
  221. // uidGetTime
  222. // Converts the date into a time string. Call through UIDate.getTime( date )
  223. //
  224. // date - the javascript date value being translated
  225. function uidGetTime( date )
  226. {
  227.     var strOut = "";
  228.     var i = 0, j = 0;
  229.     var nFormatStrLen = this.strTimeFormat.length;
  230.     var c;
  231.     var nTokenType = -1;
  232.     var nDaytimeStart = -1;
  233.     var nDaytimeLen = 0;
  234.  
  235.     for( i = 0; i < nFormatStrLen; i++ )
  236.     {
  237.         c = this.strTimeFormat.charAt(i);
  238.         nTokenType = this.strTimeTokens.indexOf( c );
  239.         if(  nTokenType != -1 )
  240.         {
  241.             // We found a token
  242.             j = i + 1;
  243.             if( c == this.strTimeFormat.charAt(j) )
  244.             {
  245.                 // double token
  246.                 j++;
  247.             }
  248.             strOut += this.uidTimeStringPartFromToken( date, nTokenType, j - i );
  249.             
  250.             // skip the token
  251.             i = j - 1;
  252.         }
  253.         else if( c == 't' )
  254.         {
  255.             // This is the daytime designation. Since we may not yet know
  256.             // if it's morning or afternoon, save the current position in
  257.             // the string, and the number of t's. Just dump the t's back
  258.             // into the string, we'll replace them later when we know
  259.             // what time of the day it is.
  260.             nDaytimeStart = strOut.length;
  261.             nDaytimeLen = 0;
  262.             // It would be nice to do-while, but 3.0 browsers choke on it
  263.             while( c == this.strTimeFormat.charAt(i + nDaytimeLen) )
  264.             {
  265.                 nDaytimeLen++;
  266.                 strOut += c;
  267.             }
  268.  
  269.             // skip the token
  270.             i += (nDaytimeLen - 1);
  271.         }
  272.         else
  273.         {
  274.             strOut += c;
  275.         }
  276.     }
  277.  
  278.     // Insert the daytime designation into the string, if we found one
  279.     if( nDaytimeStart != -1 )
  280.     {
  281.         strOut =     strOut.substring( 0, nDaytimeStart ) +
  282.                     DAYTIME[this.iDaytimeIndex] +
  283.                     strOut.substring( nDaytimeStart + nDaytimeLen );
  284.     }
  285.     return strOut;
  286. }
  287.  
  288. // uidTimeStringPartFromToken
  289. //
  290. // Returns the time string value indicted by nTokenType
  291. //
  292. // date - the date value being translated
  293. // nTokenType - the position in the token string
  294. // nTokenChars - the number of consecutive tokens in the format string
  295. function uidTimeStringPartFromToken( date, nTokenType, nTokenChars )
  296. {
  297.     var strOut = "";
  298.     var timePart;
  299.  
  300.     if( nTokenType <= 1 )
  301.     {
  302.         // Use the hour indicator to determine whether it's AM/PM and
  303.         // determine if we need to reparse
  304.         timePart = date.getHours();
  305.         if( timePart < 12 )
  306.         {
  307.             this.iDaytimeIndex = 0;
  308.         }
  309.         else
  310.         {
  311.             this.iDaytimeIndex = 1;
  312.         }
  313.         
  314.         if( nTokenType == 0 )
  315.         {
  316.             // Using 12 hour clock
  317.             if( timePart > 12 )
  318.             {
  319.                 timePart -= 12;
  320.             }
  321.         }
  322.     }
  323.     else if( nTokenType == 2 )
  324.     {
  325.         timePart = date.getMinutes();
  326.     }
  327.     else if( nTokenType == 3 )
  328.     {
  329.         timePart = date.getSeconds();
  330.     }
  331.  
  332.     if( nTokenChars > 1 && timePart < 10 )
  333.     {
  334.         strOut = "0";
  335.     }
  336.  
  337.     strOut += timePart;
  338.     return strOut;
  339. }
  340.  
  341. </SCRIPT>